package com.jkoss.wine.shop.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jkoss.wine.shop.entity.CommonType;
import com.jkoss.wine.shop.entity.MerchantCommonType;
import com.jkoss.wine.shop.entity.Shop;
import com.jkoss.wine.shop.mapper.CommonTypeMapper;
import com.jkoss.wine.shop.mapper.MerchantCommonTypeMapper;
import com.jkoss.wine.shop.mapper.ShopMapper;
import com.jkoss.wine.shop.service.IMerchantCommonTypeService;
import com.jkoss.common.util.UuidUtil;

/**
 * 服务实现类
 * 
 * @Author Supreme
 * @Version 1.0, 2018-09-29
 * @See
 * @Since com.jkoss.wine.shop.service.impl
 * @Description: TODO
 */
@Service
public class MerchantCommonTypeServiceImpl extends ServiceImpl<MerchantCommonTypeMapper, MerchantCommonType> implements IMerchantCommonTypeService {

	@Autowired
	private MerchantCommonTypeMapper merchantCommonTypeMapper;

	@Autowired
	private ShopMapper shopMapper;

	@Autowired
	private CommonTypeMapper commonTypeMapper;

	@Override
	public boolean insertCommonType(MerchantCommonType merchantCommonType) {
		int i = merchantCommonTypeMapper.insertAllColumn(merchantCommonType);
		List<Shop> shops = shopMapper.selectList(null);
		List<CommonType> commonTypes = new ArrayList<>();
		for (Shop shop : shops) {
			CommonType temp = new CommonType();
			BeanUtils.copyProperties(merchantCommonType, temp);
			temp.setParentId(merchantCommonType.getCommonTypeId());
			temp.setCommonTypeId(UuidUtil.getUUID());
			temp.setShopId(shop.getShopId());
			temp.setOperator(null);
			// 因为是创建类型，所以必定不为null
			temp.setDel(0);
			commonTypes.add(temp);
		}
		if (commonTypes.size() > 0) {
			commonTypeMapper.insertMerchantCommonType(commonTypes);
		}
		return true;
	}

	@Override
	public boolean updateCommonType(MerchantCommonType commonType) throws Exception {
		int i = merchantCommonTypeMapper.updateAllColumnById(commonType);
		if (i != 1) {
			throw new Exception("更新失败:找不到该条记录");
		}
		CommonType ct = new CommonType();
		ct.setTypeClass(commonType.getTypeClass());
		ct.setTypeName(commonType.getTypeName());
		ct.setParentId(commonType.getCommonTypeId());
		commonTypeMapper.updateCommonTypeByParentId(ct);
		return true;
	}

	@Override
	public boolean deleteCommonType(String[] id) {

		// 逻辑删除删除总店通用类型
		for (String string : id) {
			MerchantCommonType merchantCommonType = new MerchantCommonType();
			merchantCommonType.setCommonTypeId(string);
			merchantCommonType.setDel(1);
			merchantCommonTypeMapper.updateById(merchantCommonType);
		}

		// 逻辑删除门店通用类型
		for (String string : id) {
			CommonType commonType = new CommonType();
			commonType.setParentId(string);
			commonType.setDel(1);
			Wrapper wrapper = Condition.create().eq("parent_id", string);
			commonTypeMapper.update(commonType, wrapper);
		}

		return true;
	}

}
